package services

import (
	"errors"
	"github.com/gin-gonic/gin"
	"hitme/go-api/logic/form"
	"hitme/go-api/logic/repository"
	"hitme/go-api/modules/api"
	"strconv"
)

type idcardService struct {}

var IdcardService = new(idcardService)

// GetList 获取列表
func (u *idcardService) GetList (c *gin.Context, req form.IdcardGetListReqForm) (*form.IdcardGetListRespForm, error) {
	// 获取规格列表
	data := repository.IdcardRep.GetList(req.IsHot, req.SpecName, "")

	return &form.IdcardGetListRespForm{
		Row:                data,
	}, nil
}

// GetCateList 获取分类列表
func (u *idcardService) GetCateList (c *gin.Context, req form.IdcardGetCateListReqForm) (*form.IdcardGetCateListRespForm, error) {
	// 获取分类列表
	cateList := repository.IdcardRep.GetCateList()

	var res []form.IdcardGetCateListReqItem
	for _, val := range cateList {
		// 获取规格列表
		data:= repository.IdcardRep.GetListByPage(0, "", val, 4, 0)
		res = append(res, form.IdcardGetCateListReqItem{
			CateName: val,
			Datas: data,
		})
	}

	return &form.IdcardGetCateListRespForm{
		Row: res,
	}, nil
}

// GetListByCate 按照分类获取列表
func (u *idcardService) GetListByCate (c *gin.Context, req form.IdcardGetListByCateReqForm) (*form.IdcardGetListByCateRespForm, error) {
	// 获取规格列表
	data := repository.IdcardRep.GetList(0, "", req.CateName)

	return &form.IdcardGetListByCateRespForm{
		Row: data,
	}, nil
}

// EnvMakeWm 制作并检测证件照
func (u *idcardService) EnvMakeWm (c *gin.Context, req form.IdcardEnvMakeWmReqForm) (*form.IdcardEnvMakeWmRespForm, error) {
	// 获取图片规格信息
	info := repository.IdcardRep.FindById(req.Id)
	if info.Id == 0 {
		return nil, errors.New("图片规格不存在")
	}

	// 制作并检测证件照
	res, err := api.IdcardApi.EnvMakeWm(api.IdcardEnvMakeWmRequest{
		ItemId: strconv.Itoa(int(info.ItemId)),
		Image: req.File,
		Color: req.Color,
		Beauty: req.IsFair,
	});
	if err != nil {
		return nil, err
	}

	if res.Code < 0 {
		return &form.IdcardEnvMakeWmRespForm{
			Check:          0,
			CheckRes:       res.Msg,
			SpecPxWidth:    info.WidthPx,
			SpecPxHeight:   info.HeightPx,
			SpecSizeWidth:  info.WidthMm,
			SpecSizeHeight: info.HeightMm,
			Colors:         info.Colors,
		}, nil
	}
	var imageUrl string
	for _, val := range res.Data.List {
		imageUrl = val
		break
	}

	// 上传图片到oss
	uploadRes, err := api.OssApi.PutObjectRemoteUrl(imageUrl)
	if err != nil {
		return nil, errors.New("上传图片失败")
	}

	return &form.IdcardEnvMakeWmRespForm{
		Check:          1,
		ImageUrl:       uploadRes.FileUrl,
		PicId:          res.Data.PicId,
		SpecPxWidth:    info.WidthPx,
		SpecPxHeight:   info.HeightPx,
		SpecSizeWidth:  info.WidthMm,
		SpecSizeHeight: info.HeightMm,
		Colors:         info.Colors,
	}, nil
}

// CutChangeClothes 剪裁换正装
func (u *idcardService) ChangeClothWm (c *gin.Context, req form.IdcardCutChangeClothesReqForm) (*form.IdcardCutChangeClothesRespForm, error) {
	// 获取图片规格信息
	info := repository.IdcardRep.FindById(req.Id)
	if info.Id == 0 {
		return nil, errors.New("图片规格不存在")
	}

	// 剪裁换正装
	res, err := api.IdcardApi.ChangeClothWm(api.IdcardChangeClothWmRequest{
		PicId:   req.PicId,
		Color:   req.Color,
		Clothes: req.Clothes,
	});
	if err != nil {
		return nil, err
	}
	if res.Code < 0 {
		return nil, errors.New(res.Msg)
	}

	// 上传图片到oss
	uploadRes, err := api.OssApi.PutObjectRemoteUrl(res.Data.Pic)
	if err != nil {
		return nil, errors.New("上传图片失败")
	}

	return &form.IdcardCutChangeClothesRespForm{
		ImageUrl:       uploadRes.FileUrl,
		PicId:          res.Data.PicId,
		SpecPxWidth:    info.WidthPx,
		SpecPxHeight:   info.HeightPx,
		SpecSizeWidth:  info.WidthMm,
		SpecSizeHeight: info.HeightMm,
		Colors:         info.Colors,
	}, nil
}